<?php

namespace app\controller\api;

use app\common\library\Translation;
use app\model\Representative as RepresentativeModel;
use app\Request;
use think\facade\Db;

class Representative extends Base
{
    //*****全部缺少$param验证（简单实现业务流程）
    /*代办创建订单*/
    public function belowOrder(Request $request)
    {
        try {
            $param = $request->param();
            $data['on'] = date('YmdHis') . mt_rand(1000, 9999);
            $data['create_time'] = time();
            $data['service'] = $param['service'];
            $data['type'] = $param['type'];
            $data['r_user_id'] = $param['r_user_id'];
            $data['r_name'] = $param['r_name'];
            $data['user_id'] = $param['user_id'];
            $data['avatar'] = $param['avatar'];
            $data['country'] = $param['country'];
            $data['u_name'] = $param['u_name'];
            $data['u_avatar'] = $param['u_avatar'];
            $data['identity_u_name'] = $param['identity_u_name'];
            $data['u_country'] = $param['u_country'];
            if (Db::table('store_representative')->insert($data)) {
                $this->api_result(200, '下单成功', ['data' => []]);
            } else {
                $this->api_result(200, '下单失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '下单失败', ['data' => []]);
        }
    }

    public function createOrder(Request $request)
    {
        try {
            $param = $request->param();
            $data['amount'] = $param['amount'];
            $data['r_content'] = $param['r_content'];
            $data['status'] = 1;
            $data['identity_name'] = $param['identity_name'];
            $order = Db::table('store_representative')->where(['on' => $param['on']])->find();
            if (Db::table('store_representative')->where(['id' => $order['id']])->update($data)) {
                $this->api_result(200, '下单成功', ['data' => []]);
            } else {
                $this->api_result(200, '下单失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '下单失败', ['data' => []]);
        }
    }


    public function fanyi($name)
    {
        $Translation = new Translation();

        $wenzi = $Translation->languageDetect($name);
        $language = $wenzi['Lang'];
        if ($language == 'zh') {// 中 - 俄
            $translated = $Translation->textTranslate($name, $Source = 'zh', $Target = 'ru');
            $name_ru = $translated['TargetText'];
        } else if ($language == 'ru') {// 俄 - 中
            $translated = $Translation->textTranslate($name, $Source = 'ru', $Target = 'zh');
            $name_ru = $name;
            $name = $translated['TargetText'];
        } else {
            // 输入其他文字时，中俄相同
            $name_ru = $name;
        }

        return ['zh' => $name, 'ru' => $name_ru];
    }

    /*代办列表查询*/
    public function getList(Request $request)
    {
        try {
            $param = $request->param();
            $page = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;
            $page = ($page - 1) * $limit;
            $status = $param['status'] ?? 0;
            if ($param['type'] == 1) {
                $where = [
                    'r_user_id' => $param['user_id'],
                ];
            } else {
                $where = [
                    'user_id' => $param['user_id'],
                ];
            }
            $where['status'] = $status;
            $list = Db::table('store_representative')->where($where)->order('create_time desc')->limit($page, $limit)->select();
            foreach ($list as $k => $v) {
                $v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $v['service_time'] = $v['service_time'] ? date('Y-m-d H:i:s', $v['service_time']) : '';
                $v['appeal_time'] = $v['appeal_time'] ? date('Y-m-d H:i:s', $v['appeal_time']) : '';
                $list[$k] = $v;
            }
            if ($list) {
                $this->api_result(200, '请求成功', ['data' => $list]);
            } else {
                $this->api_result(200, '请求成功', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '请求失败', ['data' => []]);
        }
    }

    /*代办支付*/
    public function pay(Request $request)
    {
        try {
            $param = $request->param();
            $order = Db::table('store_representative')->where(['on' => $param['on'], 'user_id' => $param['user_id']])->find();
            $member = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $order['user_id']])->field("id,username,username_ru,avatar,score,rating,level")->find();
            if (!$order) {
                $this->api_result(0, '服务器失败', ['data' => []]);
            }
            if ($member['score'] >= $order['amount']) {
                $d['after'] = $member['score'] - $order['amount'];
                $d['before'] = $member['score'] ?? 0;
                $d['score'] = '-' . $order['amount'];
                $d['memo'] = '代办服务';
                $d['remark'] = '支付: ' . '代办';
                $d['child_user_id'] = $order['id'];
                $d['lx'] = 99;
                $d['createtime'] = time();
                $d['json'] = json_encode([]);
                $d['user_id'] = $member['id'];
                Db::connect("fast_mysql")->table("fa_user_score_log")->insert($d);
                Db::connect("fast_mysql")->table("fa_user")->where(['id' => $order['user_id']])->update(['score' => $d['after']]);
                if (Db::table('store_representative')->where(['id' => $order['id']])->update(['pay_time' => time(), 'status' => 2])) {
                    $this->api_result(200, '付款成功', ['data' => []]);
                } else {
                    $this->api_result(200, '付款失败', ['data' => []]);
                }
            } else {
                $this->api_result(201, '积分不足', ['data' => []]);
                return;
            }
        } catch (\Exception $e) {
            $this->api_result(0, '付款失败', ['data' => []]);
        }
    }

    /*代办确认结算*/
    public function settlement(Request $request)
    {
        try {
            $param = $request->param();
            if ($param['type'] == 1) {
                $where = [
                    'r_user_id' => $param['user_id'],
                ];
                $data['complete'] = 1;
            } else {
                $where = [
                    'user_id' => $param['user_id'],
                ];
                $data['status'] = 3;
            }
            $where['on'] = $param['on'];
            $order = Db::table('store_representative')->where($where)->find();
            if (Db::table('store_representative')->where(['id' => $order['id']])->update($data)) {
                $this->api_result(200, '付款成功', ['data' => []]);
            } else {
                $this->api_result(200, '付款失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '付款失败', ['data' => []]);
        }
    }

    /*代办申请售后*/
    public function service(Request $request)
    {
        try {
            $param = $request->param();
            $data['service_time'] = time();
            $data['repair'] = $param['repair'];
            $data['status'] = 4;
            $data['img'] = $param['img'];
            $data['video'] = $param['video'];
            $data['repair_state'] = 1;
            $order = Db::table('store_representative')->where(['on' => $param['on'], 'user_id' => $param['user_id']])->find();
            if (Db::table('store_representative')->where(['id' => $order['id']])->update($data)) {
                $this->api_result(200, '申请成功', ['data' => []]);
            } else {
                $this->api_result(200, '申请失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '申请失败', ['data' => []]);
        }
    }

    /*代办评价*/
    public function score(Request $request)
    {
        try {
            $param = $request->param();
            $data['evaluate'] = $param['evaluate'];
            $data['score'] = $param['score'];
            $order = Db::table('store_representative')->where(['on' => $param['on'], 'user_id' => $param['user_id']])->find();
            if (Db::table('store_representative')->where(['id' => $order['id']])->update($data)) {
                $this->api_result(200, '评价成功', ['data' => []]);
            } else {
                $this->api_result(200, '评价失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '评价失败', ['data' => []]);
        }
    }

    /*代办详情*/
    public function getInfo(Request $request)
    {
        try {
            $param = $request->param();
            if ($param['type'] == 1) {
                $where = [
                    'r_user_id' => $param['user_id'],
                ];
            } else {
                $where = [
                    'user_id' => $param['user_id'],
                ];
            }
            $where['on'] = $param['on'];
            $data = Db::table('store_representative')->where($where)->find();
            if ($data) {
                $this->api_result(200, '请求成功', ['data' => $data]);
            } else {
                $this->api_result(200, '请求失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '请求失败', ['data' => []]);
        }
    }

    /*代办申请申诉*/
    public function appeal(Request $request)
    {
        try {
            $param = $request->param();
            $data['appeal_time'] = time();
            $data['appeal'] = $param['appeal'];
            $data['status'] = 4;
            $data['imgs'] = $param['imgs'];
            $data['videos'] = $param['videos'];
            $data['compensation'] = $param['compensation'];
            $data['appeal_state'] = 1;
            $data['percentage'] = $param['percentage'];
            $order = Db::table('store_representative')->where(['on' => $param['on'], 'r_user_id' => $param['user_id']])->find();
            if (Db::table('store_representative')->where(['id' => $order['id']])->update($data)) {
                $this->api_result(200, '申诉成功', ['data' => []]);
            } else {
                $this->api_result(200, '申诉失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(0, '申诉失败', ['data' => []]);
        }
    }

    /* 获取通知*/
    public function notice(Request $request)
    {
        try {
            $param = $request->param();
            $buy_notice[0] = Db::table('store_representative')->where(['user_id' => $param['user_id'], 'buy_notice' => 0, 'status' => 1])->count();
            $buy_notice[1] = Db::table('store_representative')->where(['user_id' => $param['user_id'], 'buy_notice' => 0, 'status' => 2])->count();
            $buy_notice[2] = Db::table('store_representative')->where(['user_id' => $param['user_id'], 'buy_notice' => 0, 'status' => 3])->count();
            $buy_notice[3] = Db::table('store_representative')->where(['user_id' => $param['user_id'], 'buy_notice' => 0, 'status' => 4])->count();
            $service_notices[0] = Db::table('store_representative')->where(['r_user_id' => $param['user_id'], 'service_notices' => 0, 'status' => 0])->count();
            $service_notices[1] = Db::table('store_representative')->where(['r_user_id' => $param['user_id'], 'service_notices' => 0, 'status' => 1])->count();
            $service_notices[2] = Db::table('store_representative')->where(['r_user_id' => $param['user_id'], 'service_notices' => 0, 'status' => 2])->count();
            $service_notices[3] = Db::table('store_representative')->where(['r_user_id' => $param['user_id'], 'service_notices' => 0, 'status' => 3])->count();
            $service_notices[4] = Db::table('store_representative')->where(['r_user_id' => $param['user_id'], 'service_notices' => 0, 'status' => 4])->count();
            $this->api_result(200, '请求成功', ['data' => ['buy_notice' => $buy_notice, 'service_notices' => $service_notices]]);
        } catch (\Exception $e) {
            $this->api_result(0, '请求失败', ['data' => []]);
        }
    }

    /* 更新通知*/
    public function noticeDel(Request $request)
    {
        try {
            $param = $request->param();
            if ($param['type'] == 1) {
                $notice['service_notices'] = 1;
            } else {
                $notice['buy_notice'] = 1;
            }
            $id_arr = explode(',', $param['id']);
            foreach ($id_arr as $k => &$v) {
                if ($v) {
                    Db::table('store_representative')->where('id', $v)->update($notice);
                }
            }
            $this->api_result(200, '请求成功', ['data' => []]);
        } catch (\Exception $e) {
            $this->api_result(0, '请求失败', ['data' => []]);
        }
    }
}